package com.smarthome.web.property.controller.system;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.operationservicemanager.LaundryFeeService;
import com.smarthome.domain.bean.ShLaundryFee;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * 
* @ClassName: LaundryFeeController
* @Description: 洗衣费Controller
* @author cyj
* @date 2016年4月12日 下午9:07:22
*
 */
@Controller
@RequestMapping("/laundryFeeController")
public class LaundryFeeController {
	
	@Resource
    private LaundryFeeService laundryFeeService;
	
	@RequestMapping("/dataGrid")
    @ResponseBody
	public DataGrid getPage(ShLaundryFee laundryFee,PageHelper page){
		return laundryFeeService.qeuryDataGrid(laundryFee, page);
	}
	
	
	@RequestMapping("/addPage")
    public String addPage(HttpServletRequest request,Long companyLaundryId) {
		request.setAttribute("companyLaundryId", companyLaundryId);
        return "/system/operationServiceManager/laundryFeeAdd";
	}
	
	
	
	@RequestMapping("/add")
	@ResponseBody
	public Result add(ShLaundryFee laundryFee) {
		if(StringUtils.isBlank(laundryFee.getLaundryType())) {
			return Result.failure("请输入洗衣类型！");
		}
		if(laundryFee.getFee() == null) {
			laundryFee.setFee(0L);
		}
		if(laundryFee.getTakeDate() == null) {
			laundryFee.setTakeDate(0);
		}
		if(laundryFee.getStatus() == null) {
			return Result.failure("请选择状态！");
		}
		Date nowDate = new Date();
		laundryFee.setGmtCreate(nowDate);
		laundryFee.setGmtModify(nowDate);
		try{
			if(!laundryFeeService.checkLaundryType(laundryFee)) {
				return Result.failure("该洗衣店已有相同的洗衣类型！");
			}
			
			laundryFeeService.saveOrUpdate(laundryFee);
		} catch(Exception e){
			e.printStackTrace();
			return Result.failure(e.getMessage());
		}
		return Result.success("新增成功！");
	 }
	
	
	
	@RequestMapping("/edit")
	@ResponseBody
	public Result edit(ShLaundryFee laundryFee) {
		if(StringUtils.isBlank(laundryFee.getLaundryType())) {
			return Result.failure("请输入洗衣类型！");
		}
		if(laundryFee.getFee() == null) {
			laundryFee.setFee(0L);
		}
		if(laundryFee.getTakeDate() == null) {
			laundryFee.setTakeDate(0);
		}
		if(laundryFee.getStatus() == null) {
			return Result.failure("请选择状态！");
		}
		try{
			if(!laundryFeeService.checkLaundryType(laundryFee)) {
				return Result.failure("该洗衣店已有相同的洗衣类型！");
			}
			
			
			ShLaundryFee fee = laundryFeeService.getById(laundryFee.getId());

			BeanUtils.copyProperties(laundryFee, fee);
			
			fee.setGmtModify(new Date());
			
			
			laundryFeeService.saveOrUpdate(fee);
		} catch(Exception e){
			e.printStackTrace();
			return Result.failure(e.getMessage());
			  
		}
		return Result.success("修改成功！");
	 }
		
	  
	
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request,Long id){
		request.setAttribute("laundryFee", laundryFeeService.getById(id));
		return "/system/operationServiceManager/laundryFeeEdit";
	}
	
	
	  
	  
	 
	  @RequestMapping("/delete")
	  @ResponseBody
	  public Result delete(Long id) {
		  try{
			  laundryFeeService.delete(id);
		  } catch(Exception e){
			  return Result.failure("洗衣费被引用，不能删除");
		  }
		  return Result.success("删除成功！");
	  }
	  
	  
}
